package inabit.expressioncompiler.parser;

import inabit.compiler.parser.Parser;
import inabit.data.InabitMethod;
import inabit.data.expression.arithmetic.ArithmeticExpression;
import inabit.expressioncompiler.lexical.ExpressionLexicalAnalyzer;

import java.util.HashMap;
import java.util.Map;



/**Represents the syntactic and semantic analyzer, it analyzes the file
 * read by the lexical analyzer while execute the semantic actions when necessary*/
/**@author Rafael Rocha*/
public class ExpressionParser extends Parser<ExpressionSemanticAction>{
	
	private Map<String, InabitMethod> methods;
	private ArithmeticExpression exp;
	
	/**Receives the lexical analyzer and the context-free grammar of the language*/
	public ExpressionParser() {
		super(new ExpressionLexicalAnalyzer(), ExpressionGrammar.buildGrammar());
		methods = new HashMap<String, InabitMethod>();
		setPrintCompilation(false);
	}

	public void setMethods(Map<String, InabitMethod> userMethods) {
		this.methods = userMethods;
	}
	
	public ArithmeticExpression getCompiledExpression() {
		return exp;
	}

	@Override
	protected void initialize() {
		
	}


	@Override
	public void initAction(ExpressionSemanticAction action) {
		action.setMethods(methods);
	}


	@Override
	public void deinitAction(ExpressionSemanticAction action) {
		exp = action.getExpression();
	}
}
